// ...........................................................................................................
// (C) Copyright  1996/2008 Fuego Inc.  All Rights Reserved
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Fuego Inc.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
// Last changed on 2008-01-11 17:42:57 (-0300), by: machaval. $Revision$
// ...........................................................................................................

package com.webcamtracker.ui.actions;

import com.webcamtracker.ui.UITracker;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

public class StartVideoAction
        implements Action {
    //~ Instance fields ......................................................................................

    private boolean isStarted = false;

    private PropertyChangeListener listener;
    private UITracker manager;

    //~ Constructors .........................................................................................

    public StartVideoAction(UITracker manager) {
        this.manager = manager;
    }

    //~ Methods ..............................................................................................

    public boolean isEnabled() {
        return true;
    }

    public void setEnabled(boolean b) {
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        this.listener = listener;
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        this.listener = null;
    }

    public Object getValue(String key) {
        String result = null;

        if (key.equals(Action.NAME)) {
            if (!isStarted) {
                result = START_VIDEO;
            } else {
                result = STOP_VIDEO;
            }
        }

        return result;
    }

    public void putValue(String key, Object value) {
    }

    public void actionPerformed(ActionEvent e) {

        if (!isStarted) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File("tracker/videos/"));
            int option = chooser.showOpenDialog(manager);

            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();

                manager.getVideo().startVideo("file:" + file.getAbsolutePath());
                isStarted = true;
                listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, START_VIDEO,
                        STOP_VIDEO));
            }
        } else {
            manager.getVideo().stopVideo();
            manager.getWorkingArea().clear();
            isStarted = false;
            listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, STOP_VIDEO,
                    START_VIDEO));
        }
    }

    //~ Static fields/initializers ...........................................................................

    public static final String START_VIDEO = "Start Video";
    public static final String STOP_VIDEO = "Stop Video";
}
